"""
知识库文件夹相关数据模型
"""
from datetime import datetime
from typing import Optional, List
from enum import Enum
from pydantic import BaseModel, Field  # pyright: ignore[reportMissingImports]
from bson import ObjectId  # pyright: ignore[reportMissingImports]
from app.models.base import PyObjectId


class FolderPermission(str, Enum):
    """文件夹权限"""
    PUBLIC = "public"   # 公开
    TEAM = "team"       # 团队
    PRIVATE = "private" # 私有


class KnowledgeFolder(BaseModel):
    """知识库文件夹模型"""
    id: Optional[PyObjectId] = Field(default=None, alias="_id")
    name: str = Field(..., description="文件夹名称")
    parent_id: Optional[PyObjectId] = Field(default=None, description="父文件夹ID")
    project_id: Optional[PyObjectId] = Field(default=None, description="关联项目ID（可选）")
    description: Optional[str] = Field(default=None, description="文件夹描述")
    author_id: PyObjectId = Field(..., description="创建者ID")
    organization_id: Optional[PyObjectId] = Field(default=None, description="组织ID")
    permission: FolderPermission = Field(default=FolderPermission.PRIVATE, description="权限")
    file_count: int = Field(default=0, description="文件数量")
    created_at: datetime = Field(default_factory=datetime.utcnow, description="创建时间")
    updated_at: datetime = Field(default_factory=datetime.utcnow, description="更新时间")
    children: Optional[List['KnowledgeFolder']] = Field(default=None, description="子文件夹列表")

    class Config:
        populate_by_name = True
        arbitrary_types_allowed = True
        json_encoders = {
            ObjectId: str,
            datetime: lambda v: v.isoformat()
        }


# 前向引用解析
KnowledgeFolder.model_rebuild()

